package theFirstProject;

import day15.AccountSystem;
import day15.ClientThread;
import day15.Server;

import java.io.*;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import static theFirstProject.Mothod.*;

public
class Demo {
    static
    Map<String, Moblie> card = new HashMap<>();
    static Map<String, List<ConsumInfo>> consuminfo=new HashMap<>();

    static {
        Input();
    }

    static
    Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        one();
    }

    public static void one() {
        while (true) {
            System.out.println();
            System.out.println("**************欢迎使用兜兜5G移动业务大厅***************");
            System.out.println("1.用户登录  2.用户注册  3.使用兜兜  4.话费充值  5.资费说明  6.退出系统");
            System.out.print("请选择：");

            switch (sc.nextLine()) {
                case "1": login();break;
                case "2": register();break;
                case "3":doudou();break;
                case "4":rechargeThePhoneCredit();break;
                case "5":tariffDescription();break;
                case "6": System.out.println("退出系统成功");return;
                //打印用户信息
                case "8848":
                    for (Moblie m : card.values()) {
                    System.out.println(m.getCardNumber());
                    System.out.println(m.getPassWord());
                }break;
                default: System.out.println("输入有误，请重新输入");break;
            }
        }
    }//主界面
    static void two(String phone) {

        System.out.println(phone+"登录成功");
        while (true) {

            System.out.println();
            System.out.println("**************兜兜5G移动用户菜单***************");
            System.out.println("1.本月账单查询");
            System.out.println("2.套餐余量查询");
            System.out.println("3.打印消费详单");
            System.out.println("4.套餐变更");
            System.out.println("5.办理退网");
            System.out.println("6.聊天");
            System.out.println("请选择:(1~5选择功能，其他键返回上一级菜单)");
            switch (sc.nextLine()) {
                case "1":billInquiry(phone);break;
                case "2":marginQuery(phone);break;
                case "3":detailedConsumptionList(phone);break;
                case "4":packageChange(phone);break;
                case "5":cancelTheNetwork(phone);break;
                case "6":
                    Socket client= null;
                    try {
                        client = new Socket("localhost",9998);
                        OutputStream os = client.getOutputStream();
                        os.write(("register," + phone + "\n").getBytes());
                        Thread.sleep(300);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    ClientThread c=new ClientThread(client);
                    c.start();
                    System.out.print("请输入要聊天的号码：");
                    String targetUser = sc.nextLine();
                    if(targetUser.equals(phone)){
                        System.out.println("不能聊天自己");
                        break;
                    }else if (!Server.map.containsKey(targetUser)) {
                        System.out.println(targetUser + "不在线");
                        AccountSystem.chat(phone, client, targetUser);
                        break;
                    }
                    AccountSystem.chat(phone, client, targetUser);
                    break;
                default: return;
            }
        }
    }//用户菜单

    public static void Input(){
        try {
            File file = new File("d://IO//theFirstProject.aaa");
            if(file.exists() && file.length() > 0) {
                try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
                    card = (Map<String, Moblie>) ois.readObject();
                    consuminfo = (Map<String, List<ConsumInfo>>) ois.readObject();
                }
            } else {
                // 文件不存在时初始化空集合
                card = new HashMap<>();
                consuminfo = new HashMap<>();
            }
        } catch(ClassNotFoundException | IOException e) {
            e.printStackTrace();
            // 初始化空集合以防读取失败
            card = new HashMap<>();
            consuminfo = new HashMap<>();
        }
    }
    public static void Output(){
        try (
                FileOutputStream fos=new FileOutputStream("d://IO//theFirstProject.aaa");
                ObjectOutputStream oos=new ObjectOutputStream(fos)
                ){
            oos.writeObject(card);
            oos.writeObject(consuminfo);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
